<?php

namespace app\common\library;

use app\common\exception\UploadException;
use app\common\model\Attachment;
use fast\Random;
use FilesystemIterator;
use think\facade\Config;
use think\File;
use think\Hook;
use think\facade\Db;

defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR);
defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DIRECTORY_SEPARATOR);
defined('RUNTIME_PATH') or define('RUNTIME_PATH', ROOT_PATH . 'runtime' . DIRECTORY_SEPARATOR);
/**
 * 文件上传类
 */
class Upload
{

    /**
     * 验证码有效时长
     * @var int
     */
    protected static $expire = 120;

    /**
     * 最大允许检测的次数
     * @var int
     */
    protected static $maxCheckNums = 10;

    protected $merging = false;

    protected $chunkDir = null;

    protected $config = [];

    protected $error = '';

    /**
     * @var \think\File
     */
    protected $file = null;
    protected $fileInfo = null;

    public function __construct($file = null)
    {
        $this->config = Config::get('upload');
        $this->chunkDir = RUNTIME_PATH . 'chunks';
        if ($file) {
            $this->setFile($file);
        }
    }

    public function setChunkDir($dir)
    {
        $this->chunkDir = $dir;
    }

    public function getFile()
    {
        return $this->file;
    }

    public function setFile($file)
    {
        if (empty($file)) {
            throw new UploadException('No file upload or server upload limit exceeded');
        }

        $fileInfo = $_FILES['file'];
        $suffix = strtolower(pathinfo($fileInfo['name'], PATHINFO_EXTENSION));
        $suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
        $fileInfo['suffix'] = $suffix;//后缀
        $fileInfo['imagewidth'] = 0;
        $fileInfo['imageheight'] = 0;
        $this->file = $file;
        $this->fileInfo = $fileInfo;
    }

    protected function checkExecutable()
    {
        //禁止上传PHP和HTML文件
        if (in_array($this->fileInfo['type'], ['text/x-php', 'text/html']) || in_array($this->fileInfo['suffix'], ['php', 'html', 'htm'])) {
            throw new UploadException('Uploaded file format is limited');
        }
        return true;
    }

    protected function checkMimetype()
    {
        $mimetypeArr = explode(',', strtolower($this->config['mimetype']));
        $typeArr = explode('/', $this->fileInfo['type']);
        //验证文件后缀
        if ($this->config['mimetype'] === '*'
            || in_array($this->fileInfo['suffix'], $mimetypeArr) || in_array('.' . $this->fileInfo['suffix'], $mimetypeArr)
            || in_array($this->fileInfo['type'], $mimetypeArr) || in_array($typeArr[0] . "/*", $mimetypeArr)) {
            return true;
        }
        throw new UploadException('Uploaded file format is limited');
    }

    protected function checkImage($force = false)
    {
        //验证是否为图片文件
        if (in_array($this->fileInfo['type'], ['image/gif', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/png', 'image/webp']) || in_array($this->fileInfo['suffix'], ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'webp'])) {
            $imgInfo = getimagesize($this->fileInfo['tmp_name']);
            if (!$imgInfo || !isset($imgInfo[0]) || !isset($imgInfo[1])) {
                throw new UploadException('Uploaded file is not a valid image');
            }
            $this->fileInfo['imagewidth'] = isset($imgInfo[0]) ? $imgInfo[0] : 0;
            $this->fileInfo['imageheight'] = isset($imgInfo[1]) ? $imgInfo[1] : 0;
            return true;
        } else {
            return !$force;
        }
    }

    protected function checkSize()
    {
        preg_match('/([0-9\.]+)(\w+)/', $this->config['maxsize'], $matches);
        $size = $matches ? $matches[1] : $this->config['maxsize'];
        $type = $matches ? strtolower($matches[2]) : 'b';
        $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
        $size = (int)($size * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0));
        if ($this->fileInfo['size'] > $size) {
            throw new UploadException('File is too big (%sMiB). Max filesize: %sMiB.',
                round($this->fileInfo['size'] / pow(1024, 2), 2),
                round($size / pow(1024, 2), 2));
        }
    }

    public function getSuffix()
    {
        return $this->fileInfo['suffix'] ?: 'file';
    }

    public function getSavekey($savekey = null, $filename = null, $md5 = null)
    {
        if ($filename) {
            $suffix = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
            $suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
        } else {
            $suffix = $this->fileInfo['suffix'];
        }

        $filename = $filename ? $filename : ($suffix ? substr($this->fileInfo['name'], 0, strripos($this->fileInfo['name'], '.')) : $this->fileInfo['name']);
//        $md5 = $md5 ? $md5 : md5_file($this->fileInfo['tmp_name']);
        $md5 = $md5 ? $md5 : md5($this->fileInfo['tmp_name']);
        $replaceArr = [
            '{year}'     => date("Y"),
            '{mon}'      => date("m"),
            '{day}'      => date("d"),
            '{hour}'     => date("H"),
            '{min}'      => date("i"),
            '{sec}'      => date("s"),
            '{random}'   => GetRandStr(16),
            '{random32}' => GetRandStr(32),
            '{filename}' => substr($filename, 0, 100),
            '{suffix}'   => $suffix,
            '{.suffix}'  => $suffix ? '.' . $suffix : '',
            '{filemd5}'  => $md5,
        ];
        $savekey = $savekey ? $savekey : $this->config['savekey'];
        $savekey = str_replace(array_keys($replaceArr), array_values($replaceArr), $savekey);

        return $savekey;
    }

    /**
     * 清理分片文件
     * @param $chunkid
     */
    public function clean($chunkid)
    {
        $iterator = new \GlobIterator($this->chunkDir . DIRECTORY_SEPARATOR . $chunkid . '-*', FilesystemIterator::KEY_AS_FILENAME);
        $array = iterator_to_array($iterator);
        foreach ($array as $index => &$item) {
            $sourceFile = $item->getRealPath() ?: $item->getPathname();
            $item = null;
            @unlink($sourceFile);
        }
    }

    /**
     * 合并分片文件
     * @param string $chunkid
     * @param int    $chunkcount
     * @param string $filename
     * @return attachment|\think\Model
     * @throws UploadException
     */
    public function merge($chunkid, $chunkcount, $filename)
    {
        $filePath = $this->chunkDir . DIRECTORY_SEPARATOR . $chunkid;
//        var_dump($chunkid);
//        var_dump($chunkcount);
//        var_dump($filename);
//        var_dump($filePath);
        $completed = true;
        //检查所有分片是否都存在
        for ($i = 0; $i < $chunkcount; $i++) {
            $check_file = "{$filePath}-{$i}.part";
            if (!file_exists($check_file)) {
                $completed = false;
                break;
            }
        }

        if (!$completed) {
            $this->clean($chunkid);
            throw new UploadException('Chunk file info error');
        }

        //如果所有文件分片都上传完毕，开始合并
        $uploadPath = $filePath;

        if (!$destFile = @fopen($uploadPath, "wb")) {
            $this->clean($chunkid);
            throw new UploadException('Chunk file merge error');
        }

        if (flock($destFile, LOCK_EX)) { // 进行排他型锁定
            for ($i = 0; $i < $chunkcount; $i++) {
                $partFile = "{$filePath}-{$i}.part";
                if (!$handle = @fopen($partFile, "rb")) {
                    break;
                }
                while ($buff = fread($handle, filesize($partFile))) {
                    fwrite($destFile, $buff);
                }
                @fclose($handle);
                @unlink($partFile); //删除分片
            }

            flock($destFile, LOCK_UN);
        }
        @fclose($destFile);

//        $file = new File($uploadPath);
        $file = $_FILES['file'];
        $info = [
            'name'      => $file['name'],
            'type'      => $file['type'],
            'tmp_name'  => $file['tmp_name'],
            'error'     => 0,
            'size'      => $file['size'],
            'real_path' => $uploadPath,
        ];

//        $file->setSaveName($filename)->setUploadInfo($info);
//        $file->isTest(true);

        //重新设置文件
        $this->setFile($info);

//        unset($file);
        $this->merging = true;

        //允许大文件
        $this->config['maxsize'] = "1024G";

        return $this->upload();
    }

    /**
     * 分片上传
     * @throws UploadException
     */
    public function chunk($chunkid, $chunkindex, $chunkcount, $chunkfilesize = null, $chunkfilename = null, $direct = false)
    {

        if ($this->fileInfo['type'] != 'application/octet-stream') {
            throw new UploadException('Uploaded file format is limited');
        }

        $destDir = RUNTIME_PATH . 'chunks';
        $fileName = $chunkid . "-" . $chunkindex . '.part';
        $destFile = $destDir . DIRECTORY_SEPARATOR . $fileName;
        if (!is_dir($destDir)) {
            @mkdir($destDir, 0755, true);
        }
        if (!move_uploaded_file($this->file->getPathname(), $destFile)) {
            throw new UploadException('Chunk file write error');
        }
        $file = new File($destFile);
        $this->setFile($file);
        return $file;
    }

    /**
     * 普通上传
     * @return \app\common\model\attachment|\think\Model
     * @throws UploadException
     */
    public function upload($savekey = null)
    {
        if (empty($this->file)) {
            throw new UploadException('No file upload or server upload limit exceeded');
        }

        $this->checkSize();
        $this->checkExecutable();
        $this->checkMimetype();
//        $this->checkImage();

        $savekey = $savekey ? $savekey : $this->getSavekey();
        $savekey = '/' . ltrim($savekey, '/');
        $uploadDir = substr($savekey, 0, strripos($savekey, '/') + 1);
        $fileName = substr($savekey, strripos($savekey, '/') + 1);

//        $destDir = ROOT_PATH . 'public' . str_replace('/', DIRECTORY_SEPARATOR, $uploadDir);
        $storage = config::get('filesystem.disks.public.url') . str_replace('/', DIRECTORY_SEPARATOR, $uploadDir);
        $destDir = ROOT_PATH . 'public' . $storage;
        $sha1 = sha1($savekey);

        //如果是合并文件
        if ($this->merging) {
//            if (!$this->file->check()) {
//                throw new UploadException($this->file->getError());
//            }

            $destFile = $destDir . $fileName;
            $sourceFile = $this->file['real_path'];
//            $info = $this->file->getInfo();
            $file = $this->fileInfo;
//            $this->file = null;
            if (!is_dir($destDir)) {
                @mkdir($destDir, 0755, true);
            }

            //转移临时路径文件
            rename($sourceFile, $destFile);
//            $file->setSaveName($fileName)->setUploadInfo($info);

        } else {
            $file = $this->file->move($destDir, $fileName);
            if (!$file) {
                // 上传失败获取错误信息
                throw new UploadException($this->file->getError());
            }
        }

        $this->file = $file;
        $add = [];
        $add['url'] = $storage . $fileName;
        $add['storage'] = $storage . $fileName;
        $add['filesize'] = $this->fileInfo['size'];
        $add['mimetype'] = $this->fileInfo['suffix'];
        $add['sha1'] = $sha1;
        $add['createtime'] = time();
        $add['updatetime'] = time();
        $add['uploadtime'] = time();
        $add['imagewidth'] = $this->fileInfo['imagewidth'];
        $add['imageheight'] = $this->fileInfo['imageheight'];
        $add['imagetype'] = $this->fileInfo['type'];
        $res = Db::name('attachment')->insert($add);

//        \think\Hook::listen("upload_after", $add);
        return $res;
    }

    public function setError($msg)
    {
        $this->error = $msg;
    }

    public function getError()
    {
        return $this->error;
    }
}
